﻿using Automation.BDaq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    public class SignalMonitor
    {
        #region 避免重复计算的变量
        Form1 myform;
        private bool[] inArray = Enumerable.Repeat(true, 37).ToArray();
        private bool[] spanArray = Enumerable.Repeat(true, 37).ToArray();
        #endregion
        private InstantDiCtrl _instantDiCtrl;
        private InstantDoCtrl _instantDoCtrl;
        private readonly CancellationTokenSource _cancellationTokenSource;
        private readonly object _lock = new object();
        private bool 上料1在上料 { get; set; } = false;
        private bool 上料2在上料 { get; set; } = false;
        private bool 上料3在上料 { get; set; } = false;
        private bool 给扫码信号中 { get; set; } = false;
        public SignalMonitor(Form1 form)
        {
            myform = form;
            _instantDiCtrl = new InstantDiCtrl
            {
                SelectedDevice = new DeviceInformation("PCI-1756,BID#0")
            };
            _instantDoCtrl = new InstantDoCtrl
            {
                SelectedDevice = new DeviceInformation("PCI-1756,BID#0")
            };
            _cancellationTokenSource = new CancellationTokenSource();
        }
        // 启动信号监测任务
        public void StartMonitoring()
        {
            Task.Run(() => MonitorSignals(_cancellationTokenSource.Token));
        }
        // 停止信号监测
        public void StopMonitoring()
        {
            _cancellationTokenSource.Cancel();
            _instantDiCtrl.Dispose();
        }
        private void MonitorSignals(CancellationToken cancellationToken)
        {
            bool button_abled = true;
            bool all_start = false;
            while (!cancellationToken.IsCancellationRequested)
            {
                Thread.Sleep(10);
                byte[] portDataArray = new byte[4];

                lock (_lock)
                {
                    // 读取4组portData数据
                    for (int j = 0; j < 4; j++)
                    {
                        _instantDiCtrl.Read(j, out portDataArray[j]);
                    }
                }

                // 解析并检测信号
                bool[] currentSignalStates = ProcessSignals(portDataArray);
                #region 置顶逻辑
                //如果29为1则等待，且当27变成1时，开始整个软件运行
                #region 等待复位按钮26
                if (currentSignalStates[25] == true)
                {
                    output_updown(0, 0);
                    output_updown(1, 0);
                    output_updown(2, 0);
                    output_updown(3, 0);
                    output_updown(4, 0);
                    for (int j = 0; j < 32; j++)
                    {
                        inArray[j] = true;
                        spanArray[j] = true;
                    }
                    上料1在上料 = false;
                    上料2在上料 = false;
                    上料3在上料 = false;
                    给扫码信号中 = false;
                    continue;
                }
                #endregion

                #region 急停判定
                if (currentSignalStates[9] != true ||
                    currentSignalStates[19] != true ||
                    currentSignalStates[21] != true ||
                    currentSignalStates[23] != false
                    )
                {
                    output_updown(0, 0);
                    output_updown(1, 0);
                    output_updown(2, 0);
                    output_updown(3, 0);
                    output_updown(4, 0);
                    this.myform.port_send(0, 2);
                    myform.button3_Red();
                }
                else
                {
                    myform.button3_Green();
                }
                #endregion

                #region 手自动检测29
                if (currentSignalStates[28] == true)
                {
                    if (button_abled == true)
                    {
                        myform.all_enabled();
                        button_abled = false;
                    }
                    //禁用手动按钮
                }
                else
                {
                    if (button_abled == false)
                    {
                        myform.all_abled();
                        button_abled = true;
                    }
                    //启用手动按钮,并跳过逻辑判断
                    continue;
                }
                #endregion

                #region 等待启动按钮27
                if (currentSignalStates[26] == true && all_start == false)
                {
                    all_start = true;
                    output_updown(4, 1);
                }
                if (all_start != true)
                {
                    //未启动则跳过
                    continue;
                }
                #endregion

                #region 等待停止按钮28
                if (currentSignalStates[27] == true && all_start == true)
                {
                    all_start = false;
                    this.myform.port_send(0, 2);
                    inArray = Enumerable.Repeat(true, 37).ToArray();
                    spanArray = Enumerable.Repeat(true, 37).ToArray();
                    continue;
                }
                #endregion

                //28变成0时，停止所有电机，等待27后重启
                //复位26：按下后，所有气缸降息,手自动29：按下变1后，所有手动按钮无效,否则，所有按钮有效。
                //in10、20、22、24、30：这五个一般恒为1，当任一变成0时，急停所有轴和气缸。
                #endregion
                #region 所有的判定逻辑
                {
                    #region 上料1
                    //上料1按钮按下
                    if (currentSignalStates[2] == true && inArray[2] && spanArray[1] && spanArray[2] && !上料1在上料 && currentSignalStates[0] == true && currentSignalStates[1] == true)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1按钮按下 \r\n\r");
                        上料1在上料 = true;
                        spanArray[1] = false;
                        inArray[2] = false;
                        output_updown(0, 1);
                        //开始升起并延迟2秒滚动
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(500);
                            this.myform.port_send(1, 1);
                            inArray[2] = false;
                        }));
                    }
                    //上料1气缸下降，开始传送
                    if (currentSignalStates[1] == false && inArray[1] && inArray[2] == false && 上料1在上料)
                    {
                        inArray[1] = false;
                        inArray[2] = true;
                        //立即停止
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);//下降然后判定
                            this.myform.port_send(1, 2);
                            output_updown(0, 0);
                            spanArray[1] = true;
                            上料1在上料 = false;
                            //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1气缸下降 \r\n\r");
                            //如果上料2空闲则右移
                            if (currentSignalStates[5] == true && !上料2在上料)
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1开始初步右移 \r\n\r");
                                spanArray[2] = false;
                                this.myform.port_send(2, 1);
                            }
                            else
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1放弃初步右移 \r\n\r");
                            }
                            Thread.Sleep(2000);
                            inArray[1] = true;
                        }));
                    }
                    //本来静止，但是腾出位置，可以开始移动
                    else if (currentSignalStates[1] == false && inArray[1] && inArray[2] && currentSignalStates[5] == true
                        && !上料1在上料 && !上料2在上料 && spanArray[2] && spanArray[4])
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1由静止开始右移 \r\n\r");
                        inArray[1] = false;
                        Task.Run(new Action(() =>
                        {
                            spanArray[2] = false;
                            Thread.Sleep(1000);
                            this.myform.port_send(2, 1);
                            inArray[1] = true;
                        }));
                    }
                    //右移到位如发现2有物体则先停止
                    //currentSignalStates[0] == true表示上料1无物体，
                    // currentSignalStates[5] == false表示上料2有物体了
                    //
                    else if (currentSignalStates[0] == true && spanArray[2] == false && currentSignalStates[5] == false
                        && !上料1在上料)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1右移到位停止 \r\n\r");
                        this.myform.port_send(2, 2);
                        spanArray[2] = true;
                    }
                    //碰到右边传感器，启动下一位轮子：上料1有物体，2无物体，1在转，2没在转
                    else if (currentSignalStates[0] == false && currentSignalStates[4] == true &&
                        spanArray[2] == false && spanArray[4] == true)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料1带动后面开始转\r\n\r");
                        this.myform.port_send(4, 1);
                        spanArray[4] = false;
                    }
                    #endregion
                    #region 上料2
                    if (currentSignalStates[3] == true && inArray[3] && spanArray[2] && spanArray[3] && spanArray[4] && !上料2在上料 && currentSignalStates[4] == true && currentSignalStates[5] == true)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2按钮按下 \r\n\r");
                        上料2在上料 = true;
                        inArray[3] = false;
                        output_updown(1, 1);
                        //开始升起并延迟2秒滚动
                        Task.Run(new Action(() =>
                        {
                            spanArray[3] = false;
                            this.myform.port_send(3, 1);
                            Thread.Sleep(500);
                        }));
                    }
                    if (currentSignalStates[5] == false && inArray[5] && inArray[3] == false && 上料2在上料)
                    {
                        inArray[3] = true;
                        inArray[5] = false;
                        //立即停止
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);//下降然后判定
                            output_updown(1, 0);
                            this.myform.port_send(3, 2);
                            spanArray[3] = true;
                            上料2在上料 = false;
                            //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2下降完毕\r\n\r");
                            if (currentSignalStates[7] == true && !上料3在上料)
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2初步右移\r\n\r");
                                this.myform.port_send(4, 1);
                                spanArray[4] = false;
                            }
                            else
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2放弃初步右移\r\n\r");

                            }
                            inArray[5] = true;
                        }));
                    }
                    else if (currentSignalStates[5] == false && inArray[5] && inArray[3] == true && spanArray[4] && !上料2在上料 && !上料3在上料 && spanArray[6])
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2收到上一台送来的料 \r\n\r");
                        //收到上一台送来的料
                        inArray[5] = false;

                        Task.Run(new Action(() =>
                        {
                            if (currentSignalStates[7] == true)
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2判断可以右移 \r\n\r");
                                spanArray[4] = false;
                                Thread.Sleep(1000);
                                this.myform.port_send(4, 1);
                            }
                            else
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2判断不可以右移 \r\n\r");
                            }
                            Thread.Sleep(500);
                            inArray[5] = true;
                        }));
                    }
                    //右移到位如发现2有物体则先停止
                    //currentSignalStates[0] == true表示上料1有物体，
                    // currentSignalStates[5] == false表示上料2有物体了
                    //
                    else if (currentSignalStates[4] == true && spanArray[4] == false && currentSignalStates[7] == false && currentSignalStates[0] == true
                        && !上料2在上料)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料2右移停止\r\n\r");
                        this.myform.port_send(4, 2);
                        spanArray[4] = true;
                    }
                    //碰到右边传感器，启动下一位轮子：上料1有物体，2无物体，1在转，2没在转
                    else if (currentSignalStates[4] == false && currentSignalStates[8] == true &&
                        spanArray[4] == false && spanArray[6] == true)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上2带动后面开始转\r\n\r");
                        this.myform.port_send(6, 1);
                        spanArray[6] = false;
                    }
                    #endregion
                    #region 上料3
                    if (currentSignalStates[6] == true && inArray[6] && spanArray[4] && spanArray[5] && spanArray[6] && !上料3在上料 && currentSignalStates[7] == true && currentSignalStates[8] == true)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3按钮按下 \r\n\r");
                        上料3在上料 = true;
                        output_updown(2, 1);
                        inArray[6] = false;
                        //开始升起并延迟2秒滚动
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(5, 1);
                            spanArray[5] = false;
                            Thread.Sleep(500);
                        }));
                    }
                    if (currentSignalStates[7] == false && inArray[7] && inArray[6] == false && 上料3在上料)
                    {
                        inArray[6] = true;
                        inArray[7] = false;
                        //立即停止
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);//下降然后判定
                            //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3下降完毕\r\n\r");
                            output_updown(2, 0);
                            this.myform.port_send(5, 2);
                            spanArray[5] = true;
                            上料3在上料 = false;
                            if (currentSignalStates[11] == false)
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3初步右移\r\n\r");
                                this.myform.port_send(6, 1);
                                spanArray[6] = false;
                            }
                            else
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3放弃初步右移\r\n\r");
                            }
                            inArray[7] = true;
                        }));
                    }
                    else if (currentSignalStates[7] == false && inArray[7] && inArray[6] == true && spanArray[6])
                    {
                        //收到上一台送来的料
                        inArray[7] = false;
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3收到上一台送来的料 \r\n\r");
                        Task.Run(new Action(() =>
                        {
                            if (currentSignalStates[10] == true && currentSignalStates[11] == false)
                            {
                                spanArray[6] = false;
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3判断可以右移 \r\n\r");
                                Thread.Sleep(1000);
                                this.myform.port_send(6, 1);
                            }
                            else
                            {
                                //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3判断不可以右移 \r\n\r");
                            }
                            inArray[7] = true;
                        }));
                    }
                    //右移到位如发现右边有物体则先停止
                    //currentSignalStates[0] == true表示上料1有物体，
                    //spanArray[2] == false表示上料1右移转动中
                    // currentSignalStates[5] == false表示上料2有物体了
                    else if (currentSignalStates[8] == true && spanArray[6] == false && currentSignalStates[11] == true && currentSignalStates[4] == true
                       && !上料3在上料)
                    {
                        //File.AppendAllText("log.txt", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 上料3因为12有东西右移停止\r\n\r");
                        this.myform.port_send(6, 2);
                        spanArray[6] = true;
                    }
                    #endregion
                    #region 缓存1
                    //单独给一个扫码信号
                    if (currentSignalStates[10] == false && !给扫码信号中)
                    {
                        给扫码信号中 = true;
                        Task.Run(new Action(() =>
                        {
                            output_updown2(0, 1);
                            Thread.Sleep(1000);
                            output_updown2(0, 0);
                            给扫码信号中 = false;
                        }));
                    }
                    //前无后有，在转，停止
                    if (currentSignalStates[10] == true && currentSignalStates[11] == true && spanArray[7] == false && inArray[7]) //spanArray[7]为false代表正在运行，true为停止
                    {
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(7, 2);
                            spanArray[7] = true;
                            //output_updown2(0, 1);
                            //Thread.Sleep(1000);
                            //output_updown2(0, 0);
                        }));
                    }//触无动力有物体currentSignalStates[29] == false
                    //前有后无，停止，开始转
                    else if (currentSignalStates[10] == false && currentSignalStates[11] == false && spanArray[7]) //spanArray[7]为false代表正在运行，true为停止
                    {
                        //开始滚动
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(7, 1);
                            spanArray[7] = false;
                            Thread.Sleep(5000);
                            this.myform.port_send(6, 2);
                            spanArray[6] = true;
                        }));
                    }
                    //独立按钮19按下，且12有物体,且没在转,转且禁用8秒
                    if (currentSignalStates[29] == true && currentSignalStates[11] == true && spanArray[7] == true)
                    {
                        //开始滚动
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(7, 1);
                            spanArray[7] = false;
                            inArray[7] = false;
                            Thread.Sleep(8000);
                            inArray[7] = true;
                            Task.Run(new Action(() =>
                            {
                                this.myform.port_send(7, 2);
                                spanArray[7] = true;
                            }));
                        }));
                    }
                    //独立按钮19按下，且12有物体,且没在转,转且禁用8秒
                    else if (currentSignalStates[30] == true && currentSignalStates[29] == true && currentSignalStates[11] == true && spanArray[7] == true)
                    {
                        //开始滚动
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(7, 1);
                            spanArray[7] = false;
                            inArray[7] = false;
                            Thread.Sleep(8000);
                            inArray[7] = true;

                            Task.Run(new Action(() =>
                            {
                                this.myform.port_send(7, 2);
                                spanArray[7] = true;
                            }));
                        }));
                    }
                    #endregion
                    #region 缓存2
                    //18无物体，currentSignalStates[17] == true
                    //且13有空位,currentSignalStates[12] == false
                    //18有物体，判断下再转动

                    //18无物体，按下即转动
                    if (currentSignalStates[17] == false && currentSignalStates[31] == true && spanArray[8] && inArray[30])//为false代表正在运行，true为停止
                    {
                        //记录18状态
                        inArray[31] = true;
                        spanArray[8] = false;
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(8, 1);
                        }));
                    }
                    //18有物体，按下判断再转动
                    else if (currentSignalStates[17] == true && currentSignalStates[12] == false && currentSignalStates[31] == true && spanArray[8] && inArray[30])//为false代表正在运行，true为停止
                    {
                        //禁用3秒
                        inArray[31] = false;
                        spanArray[8] = false;
                        inArray[30] = false;
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(8, 1);
                            spanArray[13] = false;
                            this.myform.port_send(13, 1);
                            Thread.Sleep(8000);
                            inArray[30] = true;
                        }));
                    }
                    //判断啥时停，在转，和inArray[31]记录值一致
                    else if (spanArray[8] == false && currentSignalStates[17] == inArray[31] && inArray[30])//为false代表正在运行，true为停止
                    {
                        //延迟停止
                        spanArray[8] = true;
                        Task.Run(new Action(() =>
                        {
                            this.myform.port_send(8, 2);
                            spanArray[13] = false;
                            this.myform.port_send(13, 1);
                        }));
                    }
                    #endregion
                    #region 缓存3
                    //13有物体，21无物体，14没在转，13没在转，联动右移，禁用8秒
                    if (currentSignalStates[12] == true && currentSignalStates[20] == false && spanArray[13] && spanArray[14] && inArray[13])
                    {
                        inArray[13] = false;
                        Task.Run(new Action(() =>
                        {
                            spanArray[13] = false;
                            spanArray[15] = false;
                            this.myform.port_send(13, 1);
                            this.myform.port_send(15, 1);
                            Thread.Sleep(8000);
                            inArray[13] = true;
                        }));
                    }//13有物体，21无物体，14没在转，13在转，15没在转，联动右移，禁用8秒
                    else if (currentSignalStates[12] == true && currentSignalStates[20] == false && spanArray[13] == false && spanArray[14] && spanArray[15] && inArray[13])
                    {
                        inArray[13] = false;
                        Task.Run(new Action(() =>
                        {
                            spanArray[15] = false;
                            this.myform.port_send(15, 1);
                            Thread.Sleep(8000);
                            inArray[13] = true;
                        }));
                    }
                    //13有物体，21有物体，14无物体，13,14都没在转，上移，且禁用9秒
                    else if (currentSignalStates[12] == true && currentSignalStates[20] == true && currentSignalStates[13] == false && spanArray[13] == true && spanArray[14] == true && inArray[13])
                    {
                        output_updown(3, 1);
                        Thread.Sleep(1000);
                        inArray[13] = false;
                        Task.Run(new Action(() =>
                        {
                            spanArray[14] = false;
                            spanArray[12] = false;
                            this.myform.port_send(14, 1);
                            this.myform.port_send(12, 1);
                            Thread.Sleep(8000);
                            spanArray[14] = true;
                            this.myform.port_send(14, 2);
                            Task.Run(new Action(() =>
                            {
                                output_updown(3, 0);
                            }));

                            inArray[13] = true;
                        }));
                    }
                    //13有物体，21有物体，13在转，停止
                    else if (currentSignalStates[12] == true && currentSignalStates[20] == true && spanArray[13] == false && inArray[13])
                    {
                        inArray[13] = false;
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);
                            spanArray[13] = true;
                            inArray[13] = true;
                            this.myform.port_send(13, 2);
                        }));
                    }
                    //13有物体，14有物体，14在转，停止
                    else if (currentSignalStates[12] == true && currentSignalStates[13] == true && spanArray[14] == false && inArray[13])
                    {
                        Task.Run(new Action(() =>
                        {
                            spanArray[14] = true;
                            this.myform.port_send(14, 2);
                            output_updown(3, 0);
                            Thread.Sleep(1000);
                        }));
                    }
                    //13有物体，14有物体，21有物体，13或者14在转，停止
                    else if (currentSignalStates[12] == true && currentSignalStates[13] == true && currentSignalStates[20] == true && spanArray[13] == false && inArray[13])
                    {
                        spanArray[13] = true;
                        this.myform.port_send(13, 2);
                    }
                    else if (currentSignalStates[12] == true && currentSignalStates[13] == true && currentSignalStates[20] == true && spanArray[14] == false && inArray[13])
                    {
                        spanArray[14] = true;
                        this.myform.port_send(14, 2);
                        Task.Run(new Action(() =>
                        {
                            output_updown(3, 0);
                        }));
                    }
                    #endregion
                    #region 缓存4
                    //21有物体，25按下，15没在转，转动8秒，附带禁用
                    if (currentSignalStates[20] == true && currentSignalStates[24] == true && spanArray[15] && inArray[25])
                    {
                        this.myform.port_send(15, 1);
                        spanArray[15] = false;
                        inArray[25] = false;
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(8000);
                            inArray[25] = true;
                        }));
                    }
                    //21有物体，15在转，停止
                    else if (currentSignalStates[20] == true && spanArray[15] == false && inArray[25])
                    {
                        this.myform.port_send(15, 2);
                        spanArray[15] = true;
                        spanArray[13] = true;
                        this.myform.port_send(13, 2);
                    }
                    #endregion
                    #region 中转1
                    //15有物体，14有物体，在转，停转
                    if (currentSignalStates[14] == true && currentSignalStates[13] == true && spanArray[12] == false)
                    {
                        spanArray[12] = true;
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);
                            this.myform.port_send(12, 2);
                            spanArray[14] = true;
                            this.myform.port_send(14, 2);
                        }));

                    }
                    //15没有物体，14有物体，没在转，开始转
                    else if (currentSignalStates[14] == false && currentSignalStates[13] == true && spanArray[12])
                    {
                        this.myform.port_send(12, 1);
                        spanArray[12] = false;
                        this.myform.port_send(11, 1);
                        spanArray[11] = false;
                    }//15没有物体，14有物体，在转，下一轴没在转，开启下一轴
                    else if (currentSignalStates[14] == false && currentSignalStates[13] == true && spanArray[12] == false && spanArray[11])
                    {
                        this.myform.port_send(11, 1);
                        spanArray[11] = false;
                    }
                    #endregion
                    #region 中转2
                    //17有物体，15有物体，在转，停转
                    if (currentSignalStates[16] == true && currentSignalStates[14] == true && spanArray[11] == false)
                    {
                        spanArray[11] = true;
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);
                            this.myform.port_send(11, 2);
                            spanArray[12] = true;
                            this.myform.port_send(12, 2);
                        }));

                    }
                    //17没有物体，15有物体，没在转，开始转
                    else if (currentSignalStates[16] == false && currentSignalStates[14] == true && spanArray[11] && inArray[22])
                    {
                        spanArray[11] = false;
                        this.myform.port_send(11, 1);
                        spanArray[10] = false;
                        this.myform.port_send(10, 1);
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(5000);
                            spanArray[11] = true;
                            this.myform.port_send(11, 2);
                        }));
                    }//17没有物体，15有物体，在转，下一轴没在转，开启下一轴
                    else if (currentSignalStates[16] == false && currentSignalStates[14] == true && spanArray[11] == false && spanArray[10] && inArray[22])
                    {
                        this.myform.port_send(10, 1);
                        spanArray[10] = false;
                    }
                    #endregion
                    #region 中转3
                    //按钮23按下后，10未在转，9未在转,9转,持续8秒
                    if (currentSignalStates[22] == true && spanArray[10] && spanArray[9] && inArray[22])
                    {
                        inArray[22] = false;
                        spanArray[9] = false;
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(1000);
                            output_updown(4, 0);
                            Thread.Sleep(1000);
                            this.myform.port_send(9, 1);
                            Thread.Sleep(8000);
                            output_updown(4, 1);
                            inArray[22] = true;
                        }));
                    }
                    //按钮23未按下，自身17有物体，10在转，延迟停转
                    else if (currentSignalStates[16] == true && spanArray[10] == false && inArray[22])
                    {
                        spanArray[10] = true;
                        Task.Run(new Action(() =>
                        {
                            Thread.Sleep(100);
                            this.myform.port_send(10, 2);
                            this.myform.port_send(9, 2);
                            spanArray[9] = true;
                        }));
                    }
                    //9在转，停止9
                    else if (spanArray[9] == false && inArray[22])
                    {
                        spanArray[9] = true;
                        this.myform.port_send(9, 2);
                        output_updown(4, 1);
                    }
                    #endregion

                    #region 停止按钮1
                    //if (currentSignalStates[0] && inArray[0])
                    //{
                    //    inArray[0] = false;
                    //    output_updown(0, 0);
                    //    myform.port_send(1, 2);
                    //    spanArray[1] = true;
                    //    inArray[2] = true;
                    //    Task.Run(new Action(() =>
                    //    {
                    //        Thread.Sleep(2000);
                    //        inArray[0] = true;
                    //    }));

                    //}
                    //#endregion

                    //#region 停止按钮2
                    //if (currentSignalStates[4] && inArray[4])
                    //{
                    //    inArray[4] = false;
                    //    output_updown(1, 0);
                    //    myform.port_send(3, 2);

                    //    inArray[3] = true;
                    //    spanArray[3] = true;

                    //    Task.Run(new Action(() =>
                    //    {
                    //        Thread.Sleep(2000);
                    //        inArray[4] = true;
                    //    }));

                    //}
                    //#endregion

                    //#region 停止按钮3
                    //if (currentSignalStates[8] && inArray[8])
                    //{
                    //    inArray[8] = false;
                    //    output_updown(2, 0);
                    //    myform.port_send(5, 2);
                    //    spanArray[5] = true;
                    //    inArray[6] = true;

                    //    Task.Run(new Action(() =>
                    //    {
                    //        Thread.Sleep(2000);
                    //        inArray[8] = true;
                    //    }));

                    //}
                    #endregion
                }
                #endregion
                Thread.Sleep(10); // 每10毫秒读取一次
            }
        }
        // 将每个portData解析为8位，并检测32个点位的状态
        private bool[] ProcessSignals(byte[] portDataArray)
        {
            bool[] signalStates = new bool[32]; // 用于存储32个点位的状态

            for (int i = 0; i < portDataArray.Length; i++)
            {
                byte portData = portDataArray[i];
                for (int bit = 0; bit < 8; bit++)
                {
                    bool isSignalActive = (portData & (1 << bit)) != 0;
                    int signalIndex = i * 8 + bit;
                    signalStates[signalIndex] = isSignalActive; // 存储每个信号状态
                    //Console.Write($"{(isSignalActive ? "1" : "0")}");
                }
            }
            return signalStates;
        }
        public void output_updown(int num, int satate)
        {
            _instantDoCtrl.WriteBit(0, num, (byte)satate);
        }
        public void output_updown2(int num, int satate)
        {
            _instantDoCtrl.WriteBit(1, num, (byte)satate);
        }
    }
}